package com.jari.lg.service.analysis.flock;

import com.jari.lg.domain.*;
import com.jari.lg.mapper.CrossBaseMapper;
import com.jari.lg.mapper.FlockHisMapper;
import com.jari.lg.utils.BaseData;
import com.jari.lg.utils.Utils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
* 版 权 : jariec.com
* 包 名 : com.jari.lg.service.analysis.flock
* 描 述 : 生成重点区域人流数据
* 创建 时 间: 2022/3/23 10:07
* @author : lpf
*/
@Service
public class FlockService {


    @Resource
    private CrossBaseMapper crossBaseMapper;

    @Resource
    private FlockHisMapper flockHisMapper;

    @Scheduled(cron = "30 * * * * ? ")
    public void createFlockHisData() throws ParseException {

        List<FlockHis> result = new ArrayList<>();
        List<CrossBase> crossBases = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            FlockHis flockHis = new FlockHis();
            Date date = randomDate("2021-01-01","2021-12-31");
            String datetime = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").format(date);
            Date datetime1 = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").parse(datetime);
            DevBase videoDetector = BaseData.videoDetectorList.get(new Random().nextInt(BaseData.videoDetectorList.size()));
            String deviceCode = videoDetector.getDevicecode();
            String deviceName = videoDetector.getDevicename();
            CrossBaseExample crossBaseExample = new CrossBaseExample();
            CrossBaseExample.Criteria criteria = crossBaseExample.createCriteria();
            criteria.andCrosscodeEqualTo(videoDetector.getCrosscode());
            crossBases = crossBaseMapper.selectByExample(crossBaseExample);
            int num = (int) (Math.random()*(1000 - 500 + 1));

            flockHis.setId(Utils.getUUID());
            flockHis.setDevicecode(deviceCode);
            flockHis.setDevicename(deviceName);
            flockHis.setLon(crossBases.get(0).getCrosslon());
            flockHis.setLat(crossBases.get(0).getCrosslat());
            flockHis.setNum(num);
            flockHis.setDatetime(datetime1);
            result.add(flockHis);
        }
        flockHisMapper.insertBatch(result);
    }


    /**
     * @描述 生成指定时间内的随机时间
     * @时间 2022/3/22 18:20
     * @参数
     * @返回值
     * @Author lpf
     */
    private static Date randomDate(String beginDate,String endDate){
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date start = format.parse(beginDate);
            Date end = format.parse(endDate);

            if(start.getTime() >= end.getTime()){
                return null;
            }
            long date = random(start.getTime(),end.getTime());
            return new Date(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static long random(long begin,long end){
        long rtn = begin + (long)(Math.random() * (end - begin));
        if(rtn == begin || rtn == end) {
            return random(begin,end);
        }
        return rtn;
    }
}
